<?php
include 'connect_pdo.php';
	try{
					$STH = $DBH->prepare("SELECT max(spell_id) from spell where d1 is not null");
					$STH->execute();
					$maxSpell = $STH->fetch();
					
				}

				catch(PDOException $e) {
					echo $e->getMessage();
				}

$maxVal = max($maxSpellEnd[0],$maxSpell[0]);
$location = '"http://www.allmyrandomstuff.com/wow/getspelldifficulty.php"';
echo "<p><form method='post'>";
echo "<input type='text' value='$maxSpell[0]' name='start'/>";
echo '<input type="text" name="end"/>';
echo '<br/><br/><select name="range">';
echo '<option value="1">Next 1</option>';
echo '<option value="50">Next 50</option>';
echo '<option value="100">Next 100</option>';
echo '<option selected="selected" value="250">Next 250</option>';
echo '<option value="500">Next 500</option>';
echo '<option value="1000">Next 1000</option>';
echo '</select><br/>';
echo '<input type="submit" value="Go!"/>';
echo '<input type="hidden" name="submitted" value="1"/>';
echo '</form></p><p style="font-size:75%">';
echo "<input onClick='location.href=$location' type='button' value='reload'/><br/>";
echo "<br/>Highest updated record is $maxVal<br/><br/>";

/*
chance = (greySkill - yourSkill) / (greySkill - yellowSkill)
http://www.wowhead.com/spell=52021&powerrel="lvl=45"
<a href="http://www.wowhead.com/item=25697" class="q3" rel="gems=23121&amp;ench=2647&amp;pcs=25695:25696:25697&amp;lvl=45">[Felstalker Bracers]</a>
*/



if ($_POST['submitted']==1)
{

$i = $_POST['start'];
$range = $_POST['range'];
//$j = min($_POST['end'],110146);
$rangeEnd = $range + $i;

	$i = $_POST['start'];
	$j = min($rangeEnd,110146);//the end of the range is either 110146 (highest number used) or the set end, whichever is lower


	
	echo "Running for $i to $j<br/><br/>";
	
	while ($i<=$j)
	{
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,"http://www.wowhead.com/spell=".$i."&xml");
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
		curl_setopt($ch, CURLOPT_TIMEOUT, 300);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		$wowhead = curl_exec($ch);

		if (empty($wowhead))
		{
			die ("Error retrieving file");
		}

		// if (strstr($wowhead,'name')==false)
		// {
			// $wowhead = str_ireplace('<![CDATA[','', $wowhead);
			// $wowhead = strip_tags($wowhead,'<a><p><span><div><table><tr><td><th>');
			// echo "Nothing found for $i<br/>";
			// print_r($wowhead);
		// }

		else if (substr_count($wowhead, 'Difficulty: [color=')>=1)
			{
			
				$wowhead = str_ireplace('<![CDATA[','', $wowhead);
				$wowhead = strip_tags($wowhead,'<a><p><span><div><table><tr><td><th>');

				$q=1;
				//$iString = 'Difficulty: [color=r'.$q.']';
				while (stripos($wowhead,'[color=r'))
				{
					$iString = '[color=r'.$q.']';
					$start = (stripos($wowhead,$iString))+10;
					$end = stripos($wowhead,'[',$start);
					$length = $end-$start;
					$diffString = substr($wowhead,$start,$length);
					${diff.$q} = $diffString;
					//echo "Searched for $iString<br/>";
					//echo "Output $diffString<br/><br/>";
					$wowhead = str_replace($iString,'',$wowhead);
					$q++;
				}

				try{
					$STH = $DBH->prepare("UPDATE spell set d1=?,d2=?,d3=?,d4=?, datestamp=now() WHERE spell_id = ?");
					$STH->bindParam(1, $diff1);
					$STH->bindParam(2, $diff2);
					$STH->bindParam(3, $diff3);
					$STH->bindParam(4, $diff4);
					$STH->bindParam(5, $i);
					$STH->execute();
					echo "Added difficulty for $i<br/>";
				}

				catch(PDOException $e) {
					echo $e->getMessage();
				}


			}
		else 
		{//echo "No difficulty found for $i<br/>";
		}
		
		curl_close($ch);

		$i++;

	}//end statement loop
	echo "Done running<br/>";
	
		try{
					include 'connect_pdo.php';
					$STH = $DBH->prepare("SELECT max(spell_id) from spell where d1 is not null");
					$STH->execute();
					$maxSpellEnd = $STH->fetch();
					
				}

				catch(PDOException $e) {
					echo $e->getMessage();
				}
echo "<br/>Highest updated record is now $maxSpellEnd[0]<br/><br/>";
	
}//end if

echo "</p>";

?>